QuickOPC User's Guide and Reference
Installed Examples - WindowsService - WindowsService1

A Windows Service that subscribes to items from the simulation server, and logs their changes into a file.

The service:

// ReSharper disable StringLiteralTypo

// WindowsService1: A Windows Service that subscribes to items from the simulation server, and logs their changes into 
// a file.

// Install the service by running:
//      C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /i WindowsService1.exe
// If you get "Access denied" error when starting the service, change its configuration to run under Local System account.

using System.Diagnostics;
using System.IO;
using System.ServiceProcess;
using JetBrains.Annotations;
using OpcLabs.EasyOpc.DataAccess;
using OpcLabs.EasyOpc.DataAccess.OperationModel;

namespace WindowsService1
{
    public partial class Service1 : ServiceBase
    {
        const string FilePath = "C:\\Service1.txt";

        // ReSharper disable once NotNullMemberIsNotInitialized
        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            File.Create(FilePath).Close();

            easyDAClient1.SubscribeMultipleItems(
                new[]
                    {
                        new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Incrementing (1 s)", 100, null),
                        new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Ramp (10 s)", 1000, null),
                        new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_BSTR", 1000, null),
                        new DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_BOOL", 1000, null)
                    });
        }

        protected override void OnStop()
        {
            easyDAClient1.UnsubscribeAllItems();
        }

        // ReSharper disable InconsistentNaming
        private void easyDAClient1_ItemChanged(object sender, EasyDAItemChangedEventArgs e)
        // ReSharper restore InconsistentNaming
        {
            string line;
            if (e.Exception is null)
            {
                Debug.Assert(!(e.Vtq is null));
                line = $"{e.Arguments.ItemDescriptor.ItemId}: {e.Vtq.DisplayValue()}";
            }
            else
                line = $"{e.Arguments.ItemDescriptor.ItemId}: ** {e.Exception.GetBaseException()} **";

            using (var textWriter = File.AppendText(FilePath))
                textWriter.WriteLine(line);
        }
    }
}
' WindowsService1: A Windows Service that subscribes to items from the simulation server, and logs their changes into 
' a file.

' Install the service by running:
'      C:\Windows\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /i WindowsService1.exe
' If you get "Access denied" error when starting the service, change its configuration to run under Local System account.
Imports System.IO
Imports System.ServiceProcess
Imports JetBrains.Annotations
Imports OpcLabs.EasyOpc.DataAccess
Imports OpcLabs.EasyOpc.DataAccess.OperationModel

Partial Public Class Service1
    Inherits ServiceBase

    Private Const FilePath As String = "C:\Service1.txt"

    Public Sub New()
        InitializeComponent()
    End Sub

    Protected Overrides Sub OnStart(ByVal args() As String)
        File.Create(FilePath).Close()

        easyDAClient1.SubscribeMultipleItems(New DAItemGroupArguments() { _
            New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Incrementing (1 s)", 100, Nothing), _
            New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Ramp (10 s)", 1000, Nothing), _
            New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_BSTR", 1000, Nothing), _
            New DAItemGroupArguments("", "OPCLabs.KitServer.2", "Simulation.Register_BOOL", 1000, Nothing) _
        })
    End Sub

    Protected Overrides Sub OnStop()
        easyDAClient1.UnsubscribeAllItems()
    End Sub

    ' ReSharper disable InconsistentNaming
    Private Sub easyDAClient1_ItemChanged(ByVal sender As Object, ByVal e As EasyDAItemChangedEventArgs) Handles easyDAClient1.ItemChanged
        ' ReSharper restore InconsistentNaming
        Dim line As String
        If e.Exception Is Nothing Then
            Trace.Assert(e.Vtq IsNot Nothing)
            line = String.Format("{0}: {1}", e.Arguments.ItemDescriptor.ItemId, e.Vtq.DisplayValue())
        Else
            line = String.Format("{0}: ** {1} **", e.Arguments.ItemDescriptor.ItemId, e.Exception.GetBaseException())
        End If

        Using textWriter = File.AppendText(FilePath)
            textWriter.WriteLine(line)
        End Using
    End Sub
End Class

 

See Also

Conceptual